package Twitim::API::Friends;

use strict;
use warnings;

use HTML::Entities;
use base qw( Twitim::API );

sub _start {
    my ( $self, $output ) = @_;

    my $api_arg = '.json';
    $self->_getfromapi( 'statuses/friends', $api_arg, $output );
}

sub _last {
    my ( $self, $array_ref, $output, $api ) = @_;

    @{$array_ref} = sort {
        $a->{'status'}->{'id'} ||= 0;
        $b->{'status'}->{'id'} ||= 0;
        $a->{'status'}->{'id'} <=> $b->{'status'}->{'id'};
    } @{$array_ref};

    foreach my $hash_ref (@$array_ref) {
        my $time = $self->get_unixtime( $hash_ref->{'status'}{'created_at'} );
        my $jid  = $hash_ref->{'screen_name'};
        my $body = $hash_ref->{'status'}{'text'} || '';
        my $id   = $hash_ref->{'status'}{'id'};
        my $source    = $hash_ref->{'status'}{'source'};
        my $favorited = $hash_ref->{'status'}{'favorited'};

        Twitim::NetBase->set_favorites( $id, $favorited );
        Twitim::NetBase->set_username( $jid, $hash_ref->{'name'} );
        Twitim::NetBase->set_userid( $jid, $hash_ref->{'id'} );
        Twitim::NetBase->set_icon( $jid, $hash_ref->{'profile_image_url'} );

        $self->{'cb'}->{'message'}->(
            {   user_id    => $jid,
                body       => decode_entities($body),
                message_id => $id,
                unixtime   => $time,
                source     => $source,
                favorited  => $favorited,
                output     => $output
            }
        );
    }
}

1;
